package utils;

import java.io.InputStream;
import java.util.Properties;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: 读取配置信息
 * @date 2021/12/10 20:05
 */
public class ConfigUtils {

    private String exchangeName;
    private String exchangeType;
    private String queueName;
    private String bindingKey;
    private String consumer;
    private Properties properties;
    private static ConfigUtils instance;

    private ConfigUtils(){};

    public static ConfigUtils getInstance(){
        if(instance==null){
            instance = new ConfigUtils();
            instance.readConfig();
        }
        return instance;
    }

    public void readConfig(){
        properties = new Properties();
        InputStream is = ConfigUtils.class.getResourceAsStream("/config.properties");
        try {
            properties.load(is);
            this.bindingKey = (String) properties.get("BindingKey");
            this.exchangeName = (String) properties.get("ExchangeName");
            this.exchangeType = (String) properties.get("ExchangeType");
            this.queueName = (String) properties.get("QueueName");
            this.consumer = (String) properties.get("ConsumerTag");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getExchangeName() {
        return exchangeName;
    }

    public void setExchangeName(String exchangeName) {
        this.exchangeName = exchangeName;
    }

    public String getExchangeType() {
        return exchangeType;
    }

    public void setExchangeType(String exchangeType) {
        this.exchangeType = exchangeType;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getBindingKey() {
        return bindingKey;
    }

    public void setBindingKey(String bindingKey) {
        this.bindingKey = bindingKey;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public static void setInstance(ConfigUtils instance) {
        ConfigUtils.instance = instance;
    }

    public String getConsumer() {
        return consumer;
    }

    public void setConsumer(String consumer) {
        this.consumer = consumer;
    }

    @Override
    public String toString() {
        return "ConfigUtils{" +
                "exchangeName='" + exchangeName + '\'' +
                ", exchangeType='" + exchangeType + '\'' +
                ", queueName='" + queueName + '\'' +
                ", bindingKey='" + bindingKey + '\'' +
                ", consumer='" + consumer + '\'' +
                '}';
    }

    public static void main(String[] args) {
        ConfigUtils configUtils = ConfigUtils.getInstance();
        System.out.println(configUtils);
    }
}
